package com.tianqu.tms.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.dao.AucBidTypeDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.AucBidTypeDto;

public class AucBidType extends TmsEntity {

	private static final long serialVersionUID = -3284047477562867757L;
	private int Id;
	private String typeName;
	private int fareAmount;
	private int auctionDeadline;
	private int delayTime;
	private int firstExpetPrices;
	private Timestamp updateTime;//yyyy-mm-dd
	private int bidType;
	private int updateUser;
	private int cssFlag;//样式
	private String updateTimeStr;
	private int num;
	

	public static AucBidType getInstance() {
		AucBidType auc = new AucBidType();
		auc.setExisting(false);
		return auc;
	}

	/**
	 * 获得指定竞价类型信息
	 * 
	 * @param status
	 * @return
	 * @throws TmsDaoException
	 */
	public AucBidType getAucbidType(int status) throws TmsDaoException {
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		AucBidTypeDto dto = dao.getAucBidType(status);
		AucBidType auc =getaucType(dto);
		return auc;
	}

	/**
	 * entity与dto转换
	 * 
	 * @return
	 */
	private AucBidType getaucType(AucBidTypeDto dto) {
		if (dto == null)return null;
		AucBidType auc = new AucBidType();
		auc.setBidType(dto.getBidType());
		auc.setFareAmount(dto.getFareAmount());
		auc.setAuctionDeadline(dto.getAuctionDeadline());
		return auc;
	}
	
	/**
	 * 载入所有竞价模式
	 * @return
	 * @throws TmsDaoException 
	 */
	@SuppressWarnings("unchecked")
	public List loadAucBidType() throws TmsDaoException{
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		
		List aucBidTypeList = new ArrayList();
		List aucBTypeList = new ArrayList();
		aucBidTypeList = dao.loadAucBidType();
		int k=1;
		for(int i = 0; i < aucBidTypeList.size(); i++){
			AucBidType aucBidType = AucBidType.getInstance();
			aucBidType = setAucBidTypeBydto((AucBidTypeDto)aucBidTypeList.get(i),k);
			if(i%2==0){
				aucBidType.setCssFlag(1);
			}else{
				aucBidType.setCssFlag(2);
			}
			aucBTypeList.add(aucBidType);
			k++;
		}
		return aucBTypeList;
	}

	
	/**
	 * 对竞价模式进行添加
	 * @param aucBidType
	 * @throws TmsDaoException 
	 */
	public void insertAucBidType(AucBidType aucBidType) throws TmsDaoException{
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		AucBidTypeDto aucBidTypeDto = new AucBidTypeDto();
		aucBidTypeDto = aucBidTypeByEntity(aucBidType);
		dao.insertAucBidType(aucBidTypeDto);
	}
	
	
	/**
	 * 判断数据库是否有相同竞价模式
	 * @return
	 * @throws TmsDaoException 
	 */
	public boolean typeNameOnly(int typeName) throws TmsDaoException{
		boolean flag = false;
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		flag = dao.typeNameOnly(typeName);//false:有相同的竞价模式，true：没有
		
		return flag;
	}
	
	/**
	 * 根据竞价模式的id载入竞价模式
	 * @param id
	 * @return
	 * @throws TmsDaoException 
	 */
	public AucBidType loadAucBidTypeById(long id) throws TmsDaoException{
		AucBidType aucBidType = AucBidType.getInstance();
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		AucBidTypeDto aucBidTypeDto = new AucBidTypeDto();
		aucBidTypeDto = dao.getAucBidTypeById(id);
		aucBidType = setAucBidTypeBydto(aucBidTypeDto, 0);
		return aucBidType;
	}
	
	
	/**
	 * 竞价模式修改
	 * @param aucBidType
	 * @throws TmsDaoException 
	 */
	public void updateAucBidType(AucBidType aucBidType) throws TmsDaoException{
		//AucBidType aucBidTypeNew = AucBidType.getInstance();
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		AucBidTypeDto aucBidTypeDto = new AucBidTypeDto();
		aucBidTypeDto = aucBidTypeByEntity(aucBidType);
		dao.updateAucBidType(aucBidTypeDto);
	}
	
	
	/**
	 * 竞价模式删除
	 * @param id
	 * @throws TmsDaoException 
	 */
	public void deleteAucBidType(long id) throws TmsDaoException{
		AucBidTypeDao dao = (AucBidTypeDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDTYPE);
		dao.deleteAucBidType(id);
	}
	/**
	 * @param aucBidTypeDto
	 * @return
	 * dto->entity
	 */
	private AucBidType setAucBidTypeBydto(AucBidTypeDto aucBidTypeDto,int k){
		AucBidType aucBidType = AucBidType.getInstance();
		aucBidType.setId(aucBidTypeDto.getId());
		aucBidType.setBidType(aucBidTypeDto.getBidType());
		aucBidType.setDelayTime(aucBidTypeDto.getDelayTime());
		aucBidType.setFareAmount(aucBidTypeDto.getFareAmount());
		aucBidType.setAuctionDeadline(aucBidTypeDto.getAuctionDeadline());
		aucBidType.setFirstExpetPrices(aucBidTypeDto.getFirstExpetPrices());
		aucBidType.setTypeName(aucBidTypeDto.getTypeName());
		aucBidType.setUpdateTimeStr(DateUtil.getDateByTimeStamp(aucBidTypeDto.getUpdateTime()));
		aucBidType.setNum(k);
		return aucBidType;
	}
	
	
	/**
	 * aucBidType ->dto
	 * @param aucBidType
	 * @return
	 */
	private AucBidTypeDto aucBidTypeByEntity(AucBidType aucBidType){
		AucBidTypeDto dto = new AucBidTypeDto();
		dto.setTypeName(aucBidType.getTypeName());
		dto.setFareAmount(aucBidType.getFareAmount());
		dto.setAuctionDeadline(aucBidType.getAuctionDeadline());
		dto.setDelayTime(aucBidType.getDelayTime());
		dto.setFirstExpetPrices(aucBidType.getFirstExpetPrices());
		dto.setBidType(aucBidType.getBidType());
		dto.setUpdateUser(aucBidType.getUpdateUser());
		dto.setId(aucBidType.getId());
		return dto;
	}
	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public int getFareAmount() {
		return fareAmount;
	}

	public void setFareAmount(int fareAmount) {
		this.fareAmount = fareAmount;
	}

	public int getAuctionDeadline() {
		return auctionDeadline;
	}

	public void setAuctionDeadline(int auctionDeadline) {
		this.auctionDeadline = auctionDeadline;
	}

	public int getDelayTime() {
		return delayTime;
	}

	public void setDelayTime(int delayTime) {
		this.delayTime = delayTime;
	}

	public int getFirstExpetPrices() {
		return firstExpetPrices;
	}

	public void setFirstExpetPrices(int firstExpetPrices) {
		this.firstExpetPrices = firstExpetPrices;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public int getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(int updateUser) {
		this.updateUser = updateUser;
	}

	public int getBidType() {
		return bidType;
	}

	public void setBidType(int bidType) {
		this.bidType = bidType;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {

	}

	public int getCssFlag() {
		return cssFlag;
	}

	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}

	public String getUpdateTimeStr() {
		return updateTimeStr;
	}

	public void setUpdateTimeStr(String updateTimeStr) {
		this.updateTimeStr = updateTimeStr;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	
}
